﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Data;
using CPPEI.Coolzon.DataProvider;
using CPPEI.Coolzon.DataProvider.Task;
using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.ServiceImp
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class DataService : IDataService
    {
        public Data.DataResult GetData(string userIdentity, string datasource, string dataset, List<Data.DataParameter> parameters)
        {
            try
            {
                //Logger.Info("DataService", string.Format("用户{0}开始获取数据{1}!", userIdentity, datasource));
                string datasourcefile = CPPEI.Coolzon.Runtime.Metadata.Storage.GetDataSourceFile(datasource) ;//CZEnvironment.GetDataSourceMetadataFilePath(datasource, dataset);
                DataSource objdatasource = CPPEI.Coolzon.Metadata.DataSourceXmlProxy.Load(datasourcefile);
                DataSet objdataset = new DataSet();
                foreach (var odataset in objdatasource.DataSets)
                {
                    if (odataset.Name == dataset)
                    {
                        objdataset = odataset;
                        break;
                    }
                }

                List<QueryParameter> objparameters = new List<QueryParameter>();
                foreach (var dataparameter in parameters)
                {
                    QueryParameter queryparameter = new QueryParameter();
                    queryparameter.Name = dataparameter.Name;
                    queryparameter.Type = dataparameter.Type;
                    queryparameter.ValueString = dataparameter.ValueString;
                    objparameters.Add(queryparameter);
                }

                DataResult result = DataProvider.Cache.DataResultCache.GetData(userIdentity,
                    objdatasource,
                    objdataset,
                    objparameters);
                if (result == null)
                {
                    IDataProvider objidataprovider = DataProviderFactory.Create(objdatasource);
                    //result = objidataprovider.QueryData(objdatasource, objdataset, objparameters);
                    // 启动数据读取任务
                    DataLoadTask task = DataLoadTaskPool.CreateTask(userIdentity, objdatasource, objdataset, objparameters, objidataprovider);
                    result = DataLoadTaskPool.Run(task);

                    DataProvider.Cache.DataResultCache.StoreData(userIdentity,
                        objdatasource,
                        objdataset,
                        objparameters,
                        result);
                }
                return result;
            }
            catch (Exception ex)
            {
                Logger.ErrorToTag("DataService", "用户{0}获取数据{1}失败,错误信息:{2}", userIdentity, datasource, ex);
                return null;
            }
            finally
            {
                //Logger.Info("DataService", string.Format("用户{0}获取数据{1}成功！",userIdentity,datasource));
            }
        }
    }
}
